<template>
  <div class="monitoring-container">
    <el-card>
      <div slot="header" class="clearfix">
        <span>监考记录管理</span>
      </div>

      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="考试ID" prop="examId">
          <el-input v-model="queryParams.examId" placeholder="请输入考试ID" clearable />
        </el-form-item>
        <el-form-item label="场次ID" prop="sessionId">
          <el-input v-model="queryParams.sessionId" placeholder="请输入场次ID" clearable />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="queryParams.userId" placeholder="请输入用户ID" clearable />
        </el-form-item>
        <el-form-item label="事件类型" prop="eventType">
          <el-select v-model="queryParams.eventType" placeholder="请选择事件类型" clearable>
            <el-option label="切屏警告" :value="1" />
            <el-option label="强制交卷" :value="2" />
            <el-option label="异常行为" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="logList" border>
        <el-table-column label="ID" prop="id" width="80" />
        <el-table-column label="考试ID" prop="examId" width="100" />
        <el-table-column label="场次ID" prop="sessionId" width="100" />
        <el-table-column label="考生" prop="userName" width="120" />
        <el-table-column label="事件类型" width="120">
          <template slot-scope="scope">
            <el-tag :type="getEventTypeTag(scope.row.eventType)">
              {{ formatEventType(scope.row.eventType) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="截图" width="100">
          <template slot-scope="scope">
            <el-image
              v-if="scope.row.screenshot"
              style="width: 80px; height: 45px"
              :src="scope.row.screenshot"
              :preview-src-list="[scope.row.screenshot]"
              fit="cover"
            />
            <span v-else>无</span>
          </template>
        </el-table-column>
        <el-table-column label="设备信息" prop="deviceInfo" width="200" show-overflow-tooltip />
        <el-table-column label="IP地址" prop="ipAddress" width="120" />
        <el-table-column label="事件时间" prop="eventTime" width="160" />
        <el-table-column label="处理状态" width="100">
          <template slot-scope="scope">
            <el-tag :type="scope.row.handleBy ? 'success' : 'warning'">
              {{ scope.row.handleBy ? '已处理' : '未处理' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleDetail(scope.row)"
            >详情</el-button>
            <el-button
              size="mini"
              type="text"
              v-if="!scope.row.handleBy"
              @click="handleProcess(scope.row)"
            >处理</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 处理对话框 -->
    <el-dialog title="处理监考事件" :visible.sync="processDialogVisible" width="500px">
      <el-form :model="processForm" ref="processForm" label-width="80px">
        <el-form-item label="处理结果" prop="result">
          <el-input
            type="textarea"
            :rows="4"
            v-model="processForm.result"
            placeholder="请输入处理结果"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="processDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitProcess">确定</el-button>
      </div>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog title="监考记录详情" :visible.sync="detailDialogVisible" width="700px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="记录ID">{{ currentLog.id }}</el-descriptions-item>
        <el-descriptions-item label="考试ID">{{ currentLog.examId }}</el-descriptions-item>
        <el-descriptions-item label="场次ID">{{ currentLog.sessionId }}</el-descriptions-item>
        <el-descriptions-item label="考生">{{ currentLog.userName }}</el-descriptions-item>
        <el-descriptions-item label="事件类型">
          {{ formatEventType(currentLog.eventType) }}
        </el-descriptions-item>
        <el-descriptions-item label="事件时间">{{ currentLog.eventTime }}</el-descriptions-item>
        <el-descriptions-item label="设备信息" :span="2">{{ currentLog.deviceInfo }}</el-descriptions-item>
        <el-descriptions-item label="IP地址">{{ currentLog.ipAddress }}</el-descriptions-item>
        <el-descriptions-item label="处理状态">
          <el-tag :type="currentLog.handleBy ? 'success' : 'warning'">
            {{ currentLog.handleBy ? '已处理' : '未处理' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="处理人" v-if="currentLog.handleBy">
          {{ currentLog.handleByName }}
        </el-descriptions-item>
        <el-descriptions-item label="处理时间" v-if="currentLog.handleBy">
          {{ currentLog.updateTime }}
        </el-descriptions-item>
        <el-descriptions-item label="处理结果" :span="2" v-if="currentLog.handleBy">
          {{ currentLog.handleResult }}
        </el-descriptions-item>
      </el-descriptions>
      <div class="screenshot-container" v-if="currentLog.screenshot">
        <h4>截图证据：</h4>
        <el-image :src="currentLog.screenshot" fit="contain" style="max-width: 100%; max-height: 400px;" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getMonitoringLogs, handleMonitoringEvent } from '@/api/exam/monitoring'

export default {
  name: 'MonitoringLog',
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        examId: undefined,
        sessionId: undefined,
        userId: undefined,
        eventType: undefined
      },
      // 监考记录列表
      logList: [],
      // 总条数
      total: 0,
      // 处理对话框
      processDialogVisible: false,
      // 处理表单
      processForm: {
        logId: null,
        result: ''
      },
      // 详情对话框
      detailDialogVisible: false,
      // 当前记录
      currentLog: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取监考记录列表
    getList() {
      getMonitoringLogs(this.queryParams).then(response => {
        this.logList = response.rows
        this.total = response.total
      })
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.handleQuery()
    },
    // 格式化事件类型
    formatEventType(type) {
      const typeMap = {
        1: '切屏警告',
        2: '强制交卷',
        3: '异常行为'
      }
      return typeMap[type] || '未知'
    },
    // 获取事件类型标签样式
    getEventTypeTag(type) {
      const tagMap = {
        1: 'warning',
        2: 'danger',
        3: 'info'
      }
      return tagMap[type] || ''
    },
    // 查看详情
    handleDetail(row) {
      this.currentLog = row
      this.detailDialogVisible = true
    },
    // 处理记录
    handleProcess(row) {
      this.processForm.logId = row.id
      this.processForm.result = ''
      this.processDialogVisible = true
    },
    // 提交处理
    submitProcess() {
      if (!this.processForm.result) {
        this.$message.warning('请输入处理结果')
        return
      }

      handleMonitoringEvent(this.processForm.logId, this.processForm.result).then(() => {
        this.$message.success('处理成功')
        this.processDialogVisible = false
        this.getList()
      })
    }
  }
}
</script>

<style scoped>
.monitoring-container {
  padding: 20px;
}

.screenshot-container {
  margin-top: 20px;
  text-align: center;
}
</style>
